package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallSecurityConfig;
import org.linlinjava.litemall.db.service.LitemallSecurityConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/admin/security")
@Validated
public class AdminSecurityController {
    private final Log logger = LogFactory.getLog(AdminSecurityController.class);

    @Autowired
    private LitemallSecurityConfigService securityConfigService;

    @GetMapping("/config")
    public Object getConfig() {
        LitemallSecurityConfig config = securityConfigService.findDefault();
        return ResponseUtil.ok(config);
    }

    @PostMapping("/config")
    public Object saveConfig(@RequestBody LitemallSecurityConfig config) {
        try {
            securityConfigService.saveOrUpdate(config);
            return ResponseUtil.ok();
        } catch (Exception e) {
            logger.error("保存安全配置失败", e);
            return ResponseUtil.fail();
        }
    }
}